import { get, post } from "@/utils/request";
import { getToken } from "@/utils/auth";
import config from "@/config/config.json";
const apiPre = `${config.siteDomain}/api/`;
function responseHandle(res) {
  const { statusCode } = res;
  if (statusCode == 401) {
    uni.showToast({
      title: "登录已过期，请重新登录",
      icon: "none",
      duration: 2000,
      success: () => {
        setTimeout(() => {
          uni.navigateTo({
            url: "/pages/wxlogin/index",
          });
        }, 2000);
      },
    });
  }
}
/**
 * ai回答接口，这种就是流式接口
 * @param {*} data
 * @returns
 */
export function getAiAnswer(param = {}) {
  let { url = "chat_message/send", data, complete } = param;

  return uni.request({
    url: `${apiPre}${url}`,
    timeout: 300000,
    responseType: "text",
    method: "POST",
    enableChunked: true, //配置这里
    header: {
      // "content-type": "application/json", // 默认值
      Satoken: getToken(),
    },
    data,
    success: (res) => {
      responseHandle(res);
    },
    complete: (res) => {
      complete();
    },
    fail: (error) => {
      console.error("ai返回失败", error);
    },
    // success(resp) {
    //   ;
    //   let res = resp.data;
    //   console.log("ai回答", res);
    //   console.log("ai回答22", resp.data.text);
    // },
    // enableChunked: true, // 开启分片模式
  });
}

export function getAnswerList(data) {
  let { params, apiUrl } = data;
  let url = "/faqs/answerlist";

  return post({
    url: apiUrl || url,
    data: params,

    onDownloadProgress: data.onDownloadProgress,
  });
}
export function getQuestionBankAi(obj) {
  let { data, complete } = obj;
  let apiUrl = "faqs/getAnswer";
  return uni.request({
    url: `${config.siteDomain}/api/faqs/getAnswer`,
    timeout: 5000,
    responseType: "text",
    method: "POST",
    enableChunked: true, //配置这里

    header: {
      // "content-type": "application/json", // 默认值
      Satoken: getToken(),
    },
    data,
    success: (res) => {
      console.log("ai返回-success", res);
      responseHandle(res);
    },
    fail: (error) => {
      console.log("ai返回-error", error);
    },
    complete: (res) => {
      console.log("ai返回-complete", res);
      complete();
    },
    // success(resp) {
    //   ;
    //   console.log("ai返回", resp.data);
    //   let res = resp.data;
    //   console.log("ai回答", res);
    //   console.log("ai回答22", resp.data.text);
    // },
    // enableChunked: true, // 开启分片模式
  });
}
